$NG.AllReady(function (page, { useValuesChange,useDataIndexChange,useUpdateRows,useUpdateRow }) {
	const mstform = $NG.getCmpApi("p_form_zsxmydsryc");
	const dgrid1 = $NG.getCmpApi("p_form_zsxmydsryc_d");
    const monthKey = ["","u_yy","u_ey","u_sy","u_siyue","u_wy","u_ly","u_qy","u_by","u_jiuyue","u_sey","u_syy","u_sey"]
	//监听项目
    if(page.oType == 'add'||page.oType == 'edit'){
      const month_init = 1453
      $NG.updateState((updater)=>{
        updater.data.p_form_zsxmydsryc.setProps({
          u_yf:{
            label:`${new Date().getMonth()+1+1}月`,
            value:month_init+(Number(new Date().getMonth()+1) || null)+1
          }
        })
      })
        getPtInfoByPhid()
        getCntCount("p_form_zsxmydsryc","u_ht_no",()=>{
            getCntMInfoByPhid()
        })
        cntMFilter()
    }
    if(page.oType == 'add'){
        $NG.updateState((updater)=>{
            updater.data.p_form_zsxmydsryc.setProps({
                u_nd:{
                    label:new Date().getFullYear(),
                    value:new Date().getFullYear()
                }
            })
        })
        getMonthPayByCenter()
    }
	useValuesChange(({ args }) => {
    console.log("月份------------",mstform.getItem("u_yf").getValue());
		console.log(`项目监听`, args[0]);
		getPtInfoByPhid();
        getCntCount("p_form_zsxmydsryc","u_ht_no",()=>{
            getCntMInfoByPhid()
        })
        cntMFilter()
        getYearPayPlanByPhidPc()
	}, "p_form_zsxmydsryc.phid_pc");
    useValuesChange(({args})=>{
        getMonthPayByCenter()
    },"p_form_zsxmydsryc.u_qyzx")
    //监听合同
    useValuesChange(({ args }) => {
		console.log(`项目合同`, args[0]);
		getCntMInfoByPhid();
	}, "p_form_zsxmydsryc.u_ht_no");
    useUpdateRow(({args})=>{
        const map_ = args[0]
        console.log(args,map_.u_yuefen_EXName.substring(0,1));
        map_.u_jh = window._lsz_catch_monthPay[monthKey[(Number(map_.u_yuefen_EXName.substring(0,1)) || 0)]]
        if(window._lsz_catch_upYear){
          map_.u_tq = window._lsz_catch_upYear[monthKey[(Number(map_.u_yuefen_EXName.substring(0,1)) || 0)]]
          map_.u_tb = ((Number(map_.u_tq) || 0)/(Number(map_.u_jh) || 0))
        }
        map_.u_mdyc = (Number(map_.u_bqyqrsr) || 0) + (Number(map_.u_bqsyyc) || 0)
        map_.u_jyc = (Number(map_.u_mdyc) || 0) - (Number(map_.u_bqsyyc) || 0)
        map_.u_jhwcl = (Number(map_.u_mdyc) || 0)/(Number(map_.u_jh) || 0)
        map_.u_rwwcl = (Number(map_.u_mdyc) || 0)/(Number(map_.u_ycxdrw) || 0)
        dgrid1.refreshView()
        
    },"p_form_zsxmydsryc_d")
	/**
	 *
	 * 功能扩展区
	 *
	 */
	//获取项目信息
	function getPtInfoByPhid() {
		if (mstform.getItem("phid_pc").getValue()) {
			$NG.execServer(
				"getPtInfoByPhid",
				{ phid: mstform.getItem("phid_pc").getValue() },
				function (res) {
					if (res.count == 0) return;
					const datas = JSON.parse(res.data)[0].extendObjects;
					console.log(datas);
					$NG.updateState((updater) => {
						updater.data.p_form_zsxmydsryc.setProps({
							//事业部
							u_syb: {
								value: datas.division_phid,
								label: datas.division_name,
							},
							//区域中心
							u_qyzx: {
								value: datas.regional_center_phid,
								label: datas.regional_center_name,
							},
							//产品线
							product_category: {
								value: datas.midwife_phid,
								label: datas.midwife_name,
							},
						});
					});
				}
			);
		}
	}
  function getYearPayPlanByPhidPc(){
    if(!mstform.getItem("phid_pc").getValue()||!mstform.getItem("u_nd").getValue()) return
    $NG.execServer("getYearPayPlanByPhidPc",{phid_pc:mstform.getItem("phid_pc").getValue(),nd:mstform.getItem("u_nd").getValue()},(res)=>{
      if(res.count==0) return
      const _data = JSON.parse(res.data)[0].extendObjects
      $NG.updateState((updater)=>{
        updater.data.p_form_zsxmydsryc.setProps({
          u_ndzb_jh:_data.u_bnhj,
          u_ndzb_tq:_data.u_jynjh,
          u_ndzb_tb:((Number(_data.u_jynjh) || 0)/(Number(_data.u_bnhj) || 0)).toFixed(2)
        })
      })
    })
    $NG.execServer("getYearPayPlanByPhidPc",{phid_pc:mstform.getItem("phid_pc").getValue(),nd:(Number(mstform.getItem("u_nd").getValue()) || 0)-1},(res)=>{
      if(res.count==0) return
      const _data = JSON.parse(res.data)[0].extendObjects
      window._lsz_catch_upYear = {..._data}
      console.log('缓存2',window._lsz_catch_upYear);
    })
    
  }
    //获取合同相关信息
    function getCntMInfoByPhid(params) {
        $NG.execServer("getCntMInfoByPhid", { phid: mstform.getItem("u_ht_no").getValue() }, function (res) {
        if(res.count==0)return 
          const datas = JSON.parse(res.data)[0].extendObjects;
          console.log(datas);
          $NG.updateState((updater) => {
            updater.data.p_form_zsxmydsryc.setProps({
			u_htqdrq:datas.signdt,//合同签订日期
			  u_sl:datas.user_taxrate,//税率
              u_ht_name: datas.pcm_name, //合同名称
              start_dt: datas.start_dt, //约定开工时间
              end_dt: datas.end_dt, //约定完工时间
              cnt_sum_vat_fc: datas.cnt_sum_vat_fc, //合同价税合计
              //产品线
              user_product_line: { value: datas.midwife_phid, label: datas.midwife_name },
              //合同类型
              pct_id: { value: datas.pct_id, label: datas.pct_name },
              //甲方单位
              customer: { value: datas.customer_id, label: datas.customer_name },
              //乙方单位
              supply: { value: datas.supply_id, label: datas.supply_name },
              //发票类型
              invoice_type_phid: { value: datas.invoice_type_phid, label: datas.invoice_type_name },
              //购方信息（客户信息）（收票单位信息）
              //单位名称
              tick_id: { value: datas.tick_id, label: datas.tick_name },
              //单位编码
              tick_no: datas.tick_no,
              //社会信用代码
              tick_unisocialcredit: datas.tick_unisocialcredit,
              //单位地址
              tick_taxaddress: datas.tick_taxaddress,
              //联系电话
              tick_taxtelephone: datas.tick_taxtelephone,
              //银行名称
              tick_taxbankid: { value: datas.tick_taxbankid, label: datas.tick_taxbankname },
              //银行账号
              tick_taxaccountno: datas.tick_taxaccountno,
              //销方信息（供应商信息）（开票单位信息）
              //单位名称
              inv_id: { value: datas.inv_id, label: datas.inv_name },
              //单位编码
              inv_no: datas.inv_no,
              //社会信用代码
              inv_unisocialcredit: datas.inv_unisocialcredit,
              //单位地址
              inv_taxaddress: datas.inv_taxaddress,
              //联系电话
              inv_taxtelephone: datas.inv_taxtelephone,
              //银行名称
              inv_taxbankid: { value: datas.inv_taxbankid, label: datas.inv_taxbankname },
              //银行账号
              inv_taxaccountno: datas.inv_taxaccountno,
            });
            getMonthPayByCenter()
          });
        });
      }
      function getMonthPayByCenter(){
        const phid_pc = String(mstform.getItem("phid_pc").getValue())
        const center = String(mstform.getItem("u_qyzx").getValue())
        console.log("=============================",phid_pc,center);
        if(!phid_pc) return 
        $NG.execServer("getMonthPayByCenter",{phid:phid_pc},(res)=>{
            console.log(res);
            if(res.count == 0) return 
            const _data = JSON.parse(res.data)[0].extendObjects
            window._lsz_catch_monthPay = {..._data}
            console.log(`缓存`,window._lsz_catch_monthPay);
            //默认增行
    const month_init = 1453
    const currentMonth = (Number(mstform.getItem("u_yf").getValue()) || 0)
    dgrid1.clearRows().then(()=>{
        console.log('增行');
        dgrid1.addRows({
            u_yuefen:currentMonth,
            u_yuefen_EXName:(Number(currentMonth-month_init) || 0)+'月',
            u_jh:window._lsz_catch_monthPay[monthKey[3]]
        })
    })
        })
      }

	/**
	 * 过滤区
	 * */
	//合同根据项目ID过滤
	cntMFilter();
	function cntMFilter() {
		$NG.updateUI((updater, state) => {
			console.log("过滤参数检查", updater);
			updater.form.p_form_zsxmydsryc.u_ht_no.setProps({
				onBeforeOpen: async () => {
					var phid_pc = $NG
						.getCmpApi("p_form_zsxmydsryc")
						.getItem("phid_pc")
						.getValue();
					if (!phid_pc) {
						$NG.alert("请先选择项目");
						return false;
					} else {
						return true;
					}
				},
				...(!!$NG
					.getCmpApi("p_form_zsxmydsryc")
					.getItem("phid_pc")
					.getValue() && {
					clientSqlFilter: {
						'pcm3_cnt_m.bill_type': 1, //合同类型
						'pcm3_cnt_m.phid_pc': $NG
							.getCmpApi("p_form_zsxmydsryc")
							.getItem("phid_pc")
							.getValue(),
					},
				}),
			});
		});
	}
    //获取合同数量
function getCntCount(table,key,fun){
    if(!$NG.getCmpApi(table).getItem("phid_pc").getValue()) return 
    $NG.execServer("getCntCount",{type:1,phid_pc:$NG.getCmpApi(table).getItem("phid_pc").getValue()},(res)=>{
      console.log(res,'count');
      switch(res.count){
        case 0:
          return
        case 1:
          const datas = JSON.parse(res.data)[0].extendObjects
          const temp_map = {}
          temp_map[key] = datas.phid
          temp_map[`${key}_EXName`] = datas.bill_name
          $NG.updateState((updater)=>{
            updater.data[table].setProps({
              ...temp_map
            })
            setTimeout(()=>{
              if(fun){
                fun()
              }
            },200)
          })
      }
    })
  }
})